import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 演示OOM(OutOfMemoryError)
 */
public class ThreadPoolDemo9 {
    static class OOMClass{
        private byte[] bytes=new byte[1*1024*1024];
    }
    public static void main(String[] args) {
        ExecutorService service=  Executors.newCachedThreadPool();
        Object[] objects=new Object[15];
        for (int i = 0; i <15 ; i++) {
            int finalI = i;
            service.execute(()->{
                try {
                    Thread.sleep(finalI*200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                OOMClass oomClass=new OOMClass();
                objects[finalI]=oomClass;
                System.out.println("执行第"+finalI+"次");
            });
        }
    }
}
